// 查询游客信息
document.getElementById('findUsersForm').addEventListener('submit', function (e) {
    e.preventDefault();
    const option = document.getElementById('option').value;
    const value = document.getElementById('value').value;
    fetch('/api/users?option=' + option + '&value=' + value)
        .then(response => {
            if (!response.ok) {
                throw new Error('网络请求失败');
            }
            return response.json();
        })
        .then(data => {
            if (data.length > 0) {
                // 处理查询到的用户信息数据，显示在页面上
                let resultHTML = '<ul>';
                data.forEach(user => {
                    resultHTML += `<li>用户 ID:  ${user.userId}</li>
<li>用户名:  ${user.username}</li>
<li>邮箱:  ${user.email}</li>
<li>电话:  ${user.phone}</li>
`;
                });
                resultHTML += '</ul>';
                document.getElementById('queryResult').innerHTML = resultHTML;
                // alert('查询成功！');
            } else {
                alert('查询失败！未找到相关用户信息。');
            }
        })
        .catch(error => {
            console.log(error);
            alert('查询失败！错误信息：' + error.message);
        });
});



// 查询每日售票数据
document.getElementById('getDailySalesStatisticsForm').addEventListener('submit', function (e) {
    e.preventDefault();
    const selectedDate = document.getElementById('date').value;
    fetch(`/api/daily-sales-statistics?date=${selectedDate}`)
        .then(response => response.json())
        .then(data => {
            const resultDiv = document.getElementById('result');
            if (data.length > 0) {
                const statistics = data[0];
                resultDiv.innerHTML = `
                            <p>统计项ID：${statistics.statisticId}</p>

                            <p>大门门票销售数量：${statistics.gateTicketsSold}</p>
                            <p>展览 1 门票销售数量：${statistics.exhibition1TicketsSold}</p>
                            <p>展览 2 门票销售数量：${statistics.exhibition2TicketsSold}</p>
                            <p>展览 1 和 2 门票销售数量：${statistics.exhibition1And2TicketsSold}</p>
                            <p>总销售额：${statistics.totalSales}</p>
                        `;
            } else {
                resultDiv.innerHTML = "没有查询到该日期的数据。";
            }
        })
        .catch(error => {
            console.error('Error:', error);
        });
});

// 查询每月售票数据
document.getElementById('getMonthlySalesStatisticsForm').addEventListener('submit', function (e) {
    e.preventDefault();
    const startDate = document.getElementById('startDate').value;
    const endDate = document.getElementById('endDate').value;
    fetch('/api/monthly-sales-statistics?startDate=' + startDate + '&endDate=' + endDate)
        .then(response => response.json())
        .then(data => {
            const resultDiv = document.getElementById('monthresult');
            let totalGateTicketsSold = 0;
            let totalExhibition1TicketsSold = 0;
            let totalExhibition2TicketsSold = 0;
            let totalExhibition1And2TicketsSold = 0;
            let totalSales = 0;
            data.forEach(statistics => {
                totalGateTicketsSold += statistics.gateTicketsSold;
                totalExhibition1TicketsSold += statistics.exhibition1TicketsSold;
                totalExhibition2TicketsSold += statistics.exhibition2TicketsSold;
                totalExhibition1And2TicketsSold += statistics.exhibition1And2TicketsSold;
                totalSales += statistics.totalSales;
            });
            resultDiv.innerHTML = `
                        <p>大门门票销售总计：${totalGateTicketsSold}</p>
                        <p>展览 1 门票销售总计：${totalExhibition1TicketsSold}</p>
                        <p>展览 2 门票销售总计：${totalExhibition2TicketsSold}</p>
                        <p>展览 1 和 2 门票销售总计：${totalExhibition1And2TicketsSold}</p>
                        <p>总销售额总和：${totalSales}</p>
                    `;
        })
        .catch(error => console.log(error));
});

// // 查询每年售票数据
// document.getElementById('getYearlySalesStatisticsForm').addEventListener('submit', function (e) {
//     e.preventDefault();
//     const year = document.getElementById('year').value;
//     fetch('/api/yearly-sales-statistics?year=' + year)
//         .then(response => response.json())
//         .then(data => {
//             // 处理每年售票数据
//             console.log(data);
//         })
//         .catch(error => console.log(error));
// });

// 修改门票数据
document.getElementById('insertDailyTicketDataForm').addEventListener('submit', function (e) {
    e.preventDefault();
    const type = document.getElementById('type').value;
    const quantity = parseInt(document.getElementById('quantity').value);
    if (isNaN(quantity)) {
        alert('请输入有效的数量');
        return;
    }
    fetch('/api/insert-daily-ticket-data', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: `type=${type}&quantity=${quantity}`
    })
        .then(response => response.text()) // 修改这里为 response.text() 以获取字符串
        .then(data => {
            alert(data); // 直接显示后端返回的字符串
        });
});